#include "scan.h"
#include "ps2.h"
#include "control.h"
#include "delay.h"
#include "beep.h"
#include "untrasonic.h"
#include <math.h>
extern u16 ADV[128],In_Carport,Out_Carport,Stop,Barrier,Carport_Line;
extern int Motor_A,Motor_B,Servo,Target_A,Target_B,Velocity,Last_Velocity,i,j,Show_Flag,CCD_Zhongzhi,Encoder_A,Encoder_B;

void Ps2_Key_Scan(void)
{
    switch(PS2_DataKey())
			{
         case 9:
					 {
             if(Velocity<120&Stop==0&Barrier==0) Velocity+=20,Last_Velocity=Velocity;
						 break;
           }
				 case 10: 
				   {
             if(Velocity>80)  Velocity-=20,Last_Velocity=Velocity;
						 break;
           }
				 case 11: {break;}
				 case 13: {Stop=0;break;}
				 case 14: {if(Out_Carport!=2) Out_Carport=1;break;}
				 case 15: {Stop=1;break;}
				 case 16: {if(In_Carport<2) In_Carport=1;break;}
				 default:  break;
      }
return;
}

void Carport_Handle(void)
{
if(Carport_Line==1&&In_Carport==1)
		{
      TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE);
			
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			Set_Pwm(-3000,-3000,1500);
			Delay_ms(530);
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			Set_Pwm(3000,4500,1050);
			Delay_ms(1100);
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			Encoder_A=Read_Encoder(2);
		  Encoder_B=-Read_Encoder(3);
			TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
      In_Carport=2;	
			Carport_Line=0;
    }
		
			if(Carport_Line==2&&In_Carport==1)
		{
      TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE);
			
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			Set_Pwm(-3000,-3000,1500);
			Delay_ms(530);
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			Set_Pwm(4500,3000,1050);
			Delay_ms(1100);
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			Encoder_A=Read_Encoder(2);
		  Encoder_B=-Read_Encoder(3);
			TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
      In_Carport=2;	
			Carport_Line=0;
    }
			
		if(Carport_Line==1&&In_Carport==2)
		{
      TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE);
			Set_Pwm(0,0,1500);
      In_Carport=3;
      Out_Carport=0;		
			while(PS2_DataKey()!=14);
			Out_Carport=1;
    }
		if(Out_Carport==1&&In_Carport==3)
		{
      Set_Pwm(-3000,-4500,1150);
		
			
			TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE);
			
      Delay_ms(1020);
			Set_Pwm(0,0,1500);
			Delay_ms(1000);
			
			Out_Carport=2;
			In_Carport=0;
			Encoder_A=Read_Encoder(2);
		  Encoder_B=-Read_Encoder(3);
			TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
			
    }
return;
}
void Vlocity_Handle(void)
{
     if(Get_Untrasonic_Value()<200) {Velocity=0;Barrier=1;Beep_on;}	 
		 else {Barrier=0;Velocity=Last_Velocity;Beep_off;}
		 if(Stop==1) Velocity=0;
		 else if(Barrier==0)       Velocity=Last_Velocity;
}
